package com.asb.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSON;
import com.asb.model.AuditOrder;
import com.asb.model.Operator;
import com.asb.model.Order;
import com.asb.model.vo.ResultVo;
import com.asb.models.Jbpm4OasProcessHistory;
import com.asb.models.Movetasktable;
import com.asb.service.OrderService;
import com.jfinal.aop.Inject;
import com.jfinal.core.Controller;
import com.jfinal.core.Path;
import com.jfinal.ext.cors.EnableCORS;
import com.jfinal.json.FastJson;
import com.jfinal.plugin.activerecord.Page;

import cn.hutool.core.util.StrUtil;

/**
 * 处理任务工单业务
 * 
 * @author lenovo
 * @create 2022/3/21 13:29
 */
@Path("/dingtalk/inner/order")
@EnableCORS
public class OrderController extends Controller {

	@Inject
	OrderService orderService;

	// 创建单据
	public void create() {
		//
//        {"netType":"移网","PROPOSERUSER":"ln-congll12","theme":"主题","ISNEEDLEADERUSER":"是","TROUBLESPECIALTY":"专业","FEEDBACKTIME":"2022-03-22","SUMMARY":"任务摘要","LEADERUSER":"省领导账号","LEADERNAME":"省领导姓名"
//                ,"EXECUTOR":[{"STATION":"省分","REQUIREMENT":"口1","PROCESSINGUSER":"执行人账号1","PROCESSINGUSERNAME":"执行人姓名1","CITY":"","TASKTYPE":"执行","STATIONID":"1212"},{"STATION":"","REQUIREMENT":"","PROCESSINGUSER":"执行人账号2","PROCESSINGUSERNAME":"执行人姓名2","CITY":"","TASKTYPE":"执行","STATIONID":"1212"}]
//,"COPYTOR":[{"STATION":"沈阳","REQUIREMENT":"啥事","PROCESSINGUSER":"抄送人账号1","PROCESSINGUSERNAME":"抄送人姓名1","CITY":"","TASKTYPE":"抄送","STATIONID":"1212"},{"STATION":"","REQUIREMENT":"","PROCESSINGUSER":"抄送人账号2","PROCESSINGUSERNAME":"抄送人姓名2","CITY":"","TASKTYPE":"抄送","STATIONID":"1212"}]
//,"RENWUGUILEI":"任务归类"}
		String json = getRawData();
		Order order = FastJson.getJson().parse(json, Order.class);
		Map<String, Object> result = new HashMap<>();
		Map<String, Object> serviceresult = orderService.create(order);
		if ((boolean) serviceresult.get("success")) {
			Map<String, String> resultinfo = new HashMap<>();
			resultinfo.put("no", serviceresult.get("no") + "");
			result.put("code", 200);
			result.put("msg", serviceresult.get("info"));
			result.put("data", resultinfo);
		} else {
			result.put("code", 400);
			result.put("msg", serviceresult.get("info"));
		}
		this.renderJson(result);
	}

	// 修改单据
	// {"netType":"移网","PROPOSERUSER":"ln-congll12","theme":"主题","ISNEEDLEADERUSER":"是","TROUBLESPECIALTY":"专业","FEEDBACKTIME":"2022-03-22","SUMMARY":"任务摘要","LEADERUSER":"省领导账号","LEADERNAME":"省领导姓名"
	// ,"EXECUTOR":[{"STATION":"省分","REQUIREMENT":"口1","PROCESSINGUSER":"执行人账号1","PROCESSINGUSERNAME":"执行人姓名1","CITY":"","TASKTYPE":"执行","STATIONID":"1212"},{"STATION":"","REQUIREMENT":"","PROCESSINGUSER":"执行人账号2","PROCESSINGUSERNAME":"执行人姓名2","CITY":"","TASKTYPE":"执行","STATIONID":"1212"}]
	// ,"COPYTOR":[{"STATION":"沈阳","REQUIREMENT":"啥事","PROCESSINGUSER":"抄送人账号1","PROCESSINGUSERNAME":"抄送人姓名1","CITY":"","TASKTYPE":"抄送","STATIONID":"1212"},{"STATION":"","REQUIREMENT":"","PROCESSINGUSER":"抄送人账号2","PROCESSINGUSERNAME":"抄送人姓名2","CITY":"","TASKTYPE":"抄送","STATIONID":"1212"}]
	// ,"RENWUGUILEI":"任务归类"}
	public void update() {
		//
		Long id = this.getParaToLong("id");
		// 获取json对象
		String json = getRawData();
		Order order = FastJson.getJson().parse(json, Order.class);
		order.setId(id);
		Map<String, Object> result = new HashMap<>();
		Map<String, Object> update = orderService.update(order);
		if ((boolean) update.get("success")) {
			result.put("code", 200);
			result.put("msg", "执行成功");
		} else {
			result.put("code", 400);
			result.put("msg", update.get("info"));
		}
		this.renderJson(result);
	}

	// 审批单据
	// {"isleaderpass":"是","leaderopinion":"没什么意见","leaderuser":"heyan6","leadername":"领导名称"}
	public void audit() {
		Map<String, Object> result = new HashMap<>();
		if (StrUtil.isEmpty(getPara("id"))) {
			result.put("code", 400);
			result.put("msg", "请输入ID");
			this.renderJson(result);
		}
		Long id = Long.valueOf(getPara("id"));
		String json = getRawData();
		AuditOrder order = FastJson.getJson().parse(json, AuditOrder.class);
		order.setId(id);

		Map<String, Object> audit = orderService.audit(order);
		if ((boolean) audit.get("success")) {
			result.put("code", 200);
			result.put("msg", "执行成功");
		} else {
			result.put("code", 400);
			result.put("msg", audit.get("info"));
		}
		this.renderJson(result);
	}

	// 关闭单据
	// {"user":"dw-hubing"}
	public void close() {
		Long id = getParaToLong("id");
		Operator operator = new Operator();
		operator.setId(id);
		Map<String, Object> result = new HashMap<>();
		Map<String, Object> close = orderService.close(operator);
		if ((boolean) close.get("success")) {
			result.put("code", 200);
			result.put("msg", "执行成功");
		} else {
			result.put("code", 400);
			result.put("msg", close.get("info"));
		}
		this.renderJson(result);
	}

	// 执行单据处理
	// {"processinguser":"dw-hubing","executoropinion":"意见看看","feedbacktime":"2022-03-25"}
	public void executorHandle() {
		Long id = Long.valueOf(getPara("id") + "");
		Long sid = Long.valueOf(getPara("sid") + "");
		String json = getRawData();
		Operator operator = FastJson.getJson().parse(json, Operator.class);
		operator.setId(id);
		operator.setSid(sid);
		Map<String, Object> result = new HashMap<>();
		Map<String, Object> resultsub = orderService.execute(operator);
		if ((boolean) resultsub.get("success")) {
			result.put("code", 200);
			result.put("msg", "执行成功");
		} else {
			result.put("code", 400);
			result.put("msg", resultsub.get("info"));
		}
		this.renderJson(result);
	}

	// 抄送单据处理
	// {"processinguser":"dw-hubing","executoropinion":"意见看看","feedbacktime":"2022-03-25"}
	public void copytorHandle() {
		Long id = Long.valueOf(getPara("id"));
		Long sid = Long.valueOf(getPara("sid"));
		String json = getRawData();
		Operator operator = FastJson.getJson().parse(json, Operator.class);
		operator.setId(id);
		operator.setSid(sid);
		Map<String, Object> result = new HashMap<>();
		Map<String, Object> resultsub = orderService.copyHandle(operator);
		if ((boolean) resultsub.get("success")) {
			result.put("code", 200);
			result.put("msg", "执行成功");
		} else {
			result.put("code", 400);
			result.put("msg", resultsub.get("info"));
		}
		this.renderJson(result);
	}

	// 执行人转发
	// {"processinguser":"dw-hubing","executor":{"STATION":"省分","REQUIREMENT":"口1","processinguser":"执行人账号1","processingusername":"执行人姓名1","city":"","tasktype":"执行","stationid":"1212"}}
	public void executorRedeploy() {
		Long id = Long.valueOf(getPara("id"));
		Long sid = Long.valueOf(getPara("sid"));
		String json = getRawData();
		Operator operator = FastJson.getJson().parse(json, Operator.class);
		operator.setId(id);
		operator.setSid(sid);
		Map<String, Object> result = new HashMap<>();
		if (null == operator || null == operator.getExecutor() || operator.getExecutor().size() < 1) {
			result.put("code", 400);
			result.put("msg", "请添加转派人");
			this.renderJson(result);
		}
		Map<String, Object> resultsub = orderService.executorRedeploy(operator);
		if ((boolean) resultsub.get("success")) {
			result.put("code", 200);
			result.put("msg", resultsub.get("info"));
		} else {
			result.put("code", 400);
			result.put("msg", resultsub.get("info"));
		}
		this.renderJson(result);
	}

	// 抄送人转发
	// {"processinguser":"dw-hubing","copytor":{"STATION":"省分","REQUIREMENT":"口1","processinguser":"执行人账号1","processingusername":"执行人姓名1","city":"","tasktype":"执行","stationid":"1212"}}
	public void copytorRedeploy() {
		Long id = Long.valueOf(getPara("id"));
		Long sid = Long.valueOf(getPara("sid"));
		String json = getRawData();
		Operator operator = FastJson.getJson().parse(json, Operator.class);
		operator.setId(id);
		operator.setSid(sid);
		Map<String, Object> result = new HashMap<>();
		if (null == operator || null == operator.getExecutor() || operator.getExecutor().size() < 1) {
			result.put("code", 202);
			result.put("msg", "请添加转派人");
			this.renderJson(result);
		}

		Map<String, Object> resultsub = orderService.copytorRedeploy(operator);
		if ((boolean) resultsub.get("success")) {
			result.put("code", 200);
			result.put("msg", "执行成功");
		} else {
			result.put("code", 400);
			result.put("msg", resultsub.get("info"));
		}
		this.renderJson(result);
	}

	// 查询主表单据
	public void list() {
		int pageNumber = getParaToInt("pageNumber", 1);
		int pageSize = getParaToInt("pageSize", 12);
		Map<String, String[]> paras = getParaMap();
		Map<String, String> p = new HashMap<>();
		paras.forEach((k, v) -> {
			p.put(k, v[0]);
		});
		Page<Movetasktable> page = orderService.list(p, pageNumber, pageSize);
		renderJson(new ResultVo().setData(page));
	}

	// 查询主表单据
//    http://localhost:8181/order/detail
//表头    id=12345
	public void detail() {
		Long id = Long.valueOf(getPara("id"));
		Map<String, Object> detail = orderService.detail(id);
		this.renderText(JSON.toJSONString(new ResultVo().setData(detail)));
	}

	// 查询主表单据
//    http://localhost:8181/order/history
//表头    id=12345
	public void history() {
		long id = this.getParaToLong("id");
		List<Jbpm4OasProcessHistory> list = orderService.history(id);
		renderText(JSON.toJSONString(new ResultVo().setData(list)));

	}
}
